使用 objectMapper 将 JSON 日期格式反序列化为 ZonedDateTime

您所在的位置:网站首页 jackson localdate 使用 objectMapper 将 JSON 日期格式反序列化为 ZonedDateTime

使用 objectMapper 将 JSON 日期格式反序列化为 ZonedDateTime

#使用 objectMapper 将 JSON 日期格式反序列化为 ZonedDateTime| 来源: 网络整理| 查看: 265

背景

我有以下 JSON(来自 Kafka 的消息)

{

      "markdownPercentage": 20,

      "currency": "SEK",

      "startDate": "2019-07-25"

}

我有以下(生成的 JSON 模式)POJO(我无法更改 POJO,因为它是公司的共享资源)

public class Markdown {

    @JsonProperty("markdownPercentage")

    @NotNull

    private Integer markdownPercentage = 0;

    @JsonProperty("currency")

    @NotNull

    private String currency = "";

    @JsonFormat(

        shape = Shape.STRING,

        pattern = "yyyy-MM-dd"

    )

    @JsonProperty("startDate")

    @NotNull

    private ZonedDateTime startDate;

    // Constructors, Getters, Setters etc.

}

我们的应用程序是一个 Spring Boot 应用程序,它使用 Spring Cloud Stream 从 Kafka 读取 JSON 消息 (1) 并使用 POJO (2) 然后对其进行处理。

问题

当应用程序尝试将消息反序列化为对象时,它会抛出异常

当前代码

我定义了以下 objectMapper

/**

     * Date mapper.

     *

     * @return the {@link ObjectMapper}

     */

    @Bean

    public ObjectMapper objectMapper() {

        ObjectMapper mapper = new ObjectMapper();

        mapper.registerModule(new JavaTimeModule());

        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

        return mapper;

    }

问题

我知道 POJO 中生成的 ZonedDateTime 需要源消息中不存在的“时间”元素。我只能控制 objectMapper。是否有任何可能的配置可以使这项工作?

笔记

如果反序列化 POJO 中的时间元素“假定”为 startOfDay,即“00.00.00.000Z”,我很好



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3